package com.bjsxt.livegoods.buytime.service.service.impl;

import com.bjsxt.livegoods.buytime.service.service.BuytimeService;
import com.bjsxt.livegoods.dao.product.ProductDao;
import com.bjsxt.livegoods.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BuytimeServiceImpl implements BuytimeService {
    @Autowired
    private ProductDao productDao;
    @Override
    public Map<String, Object> buytime(String id) {
        Map<String,Object> result=new HashMap<>();
        //必须是转态未出租
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(id));
        query.addCriteria(Criteria.where("status").is("2"));
        List<Product> productList=productDao.findProducts(query);
        if(productList.size()==0){
            //商品已出租，不能订购
            result.put("status","200");
            long time=System.currentTimeMillis();
            result.put("time",time+(365*24*60*60*1000));
        }else{
            //商品未出租，可以订购
            result.put("status","200");
            result.put("time",productList.get(0).getSalesTime().getTime());
        }
        return result;
    }
}
